$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_xzykjxmgl_m")
    const dgrid = $NG.getCmpApi("p_form_xzykjxmgl_d1")

    if (page.oType == 'add' || page.oType == 'edit') {
        const quarter = getCurrentQuarter()
        const year = new Date().getFullYear()
        $NG.updateState((updater) => {
            updater.data.p_form_xzykjxmgl_m.setProps({
                u_nd: {
                    label: year,
                    value: year
                },
                u_jd: {
                    label: quarter.name,
                    value: quarter.value
                }
            })
        })
    }

    useBeforeClick(() => {
        const rows = dgrid.getRows()
        let lock = []
        rows.forEach((item, index) => {
            if (item.u_lock == '1'&& item.checked) {
                lock.push(index + 1)
            }
        })
        if (lock.length !== 0) {
            $NG.alert("不能删除上个季度的数据！行号：" + lock.join('、'))
            return false
        }
    }, "deleterow")

    // 拉取相关季度明细
    useValuesChange(() => {
getDetail()
    }, "p_form_xzykjxmgl_m.phid_pc")
    useValuesChange(() => {
getDetail()
    }, "p_form_xzykjxmgl_m.u_jd")

    function getDetail() {
        debugger
        let jd = (Number(mstform.getItem("u_jd").getValue()) || 0)
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if(!jd || !phid_pc) return
        if(jd == '6870000000000382') return 
        jd = (Number(mstform.getItem("u_jd").getValue()) || 0) -1
        $NG.execServer("getTechProject", { phid_pc, jd }, (res) => {
            if (!res.count) return
            const datas = JSON.parse(res.data).reduce((value, item) => {
                item.extendObjects.u_lock = 1
                Object.keys(item.extendObjects).forEach(it => {
                    if (it.includes("exname")) {
                        item.extendObjects[it.replace("exname", "EXName")] = item.extendObjects[it]
                        delete item.extendObjects[it]
                    }
                })
                value.push(item.extendObjects)
                return value
            }, [])
            console.log(`要添加的数据`, datas);
            let temp = dgrid.getRows().filter(item=>{
                if(item.u_lock != '1'){
                    return item
                }
            })
            temp =[...temp,...datas]
            dgrid.clearRows().then(()=>{
                console.log(`添加`,datas);
                dgrid.addRows(datas)
            })
        })
    }
    // 获取当前季度
    function getCurrentQuarter() {
        const date = new Date();
        const month = date.getMonth() + 1; // 月份从0开始，所以要加1
        let quarterName, quarterValue;

        if (month >= 1 && month <= 3) {
            quarterName = "第一季度";
            quarterValue = "6870000000000382";
        } else if (month >= 4 && month <= 6) {
            quarterName = "第二季度";
            quarterValue = "6870000000000383";
        } else if (month >= 7 && month <= 9) {
            quarterName = "第三季度";
            quarterValue = "6870000000000384";
        } else {
            quarterName = "第四季度";
            quarterValue = "6870000000000385";
        }

        return {
            name: quarterName,
            value: quarterValue
        };
    }
})